package com.miao.util.collection;


import java.util.HashSet;
import java.util.Set;

public class SetUtils {

    public static Set some(Set set1, Set set2) {
        if (set1 == null) {
            set1 = new HashSet();
        }
        if (set2 == null) {
            set2 = new HashSet();
        }
        Set result = new HashSet();
        result.addAll(set1);
        result.retainAll(set2);
        return result;
    }

    public static Set diff(Set set1, Set set2) {
        if (set1 == null) {
            set1 = new HashSet();
        }
        if (set2 == null) {
            set2 = new HashSet();
        }
        Set result = new HashSet();
        result.addAll(set1);
        result.removeAll(set2);
        return result;
    }

    public static Set all(Set set1, Set set2) {
        if (set1 == null) {
            set1 = new HashSet();
        }
        if (set2 == null) {
            set2 = new HashSet();
        }
        Set result = new HashSet();
        result.addAll(set1);
        result.addAll(set2);
        return result;
    }


    public static void main(String[] args) {
        Set<String> result = new HashSet<String>();

        Set<String> set1 = new HashSet<String>() {{
            add("2222");
            add("333");
        }};

        Set<Integer> set2 = new HashSet<Integer>() {{
        }};

        System.out.println("交集：" + SetUtils.some(set1, set2));

        System.out.println("差集：" + SetUtils.diff(set1, set2));

        System.out.println("差集：" + SetUtils.diff(set2, set1));

        System.out.println("并集：" + SetUtils.all(set1, set2));

    }

}
